package bg.cinema_system.beans;

import java.sql.SQLException;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import bg.cinema_system.service.UserTicketsService;

@ManagedBean(name = "admin")
@SessionScoped
public class AdminBean {

	private String userRole;
	private String newUserRole;
	private String username;
	private String email;
	private String message;
	private String error;
	private boolean validUsername;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) throws SQLException {
		this.username = username;

		String role = UserTicketsService.getRole(this.username);

		if ("user".equals(role)) {
			this.userRole = "Потребител";
		} else if ("staff".equals(role)) {
			this.userRole = "Персонал";
		} else {
			this.userRole = "Администратор";
		}

		this.email = UserTicketsService.getEmail(this.username);

		if (email != null && !"".equals(email)) {
			this.message = null;
			this.error = null;
			this.validUsername = true;
		} else {
			this.validUsername = false;
		}

		this.username.replaceAll("<", "&gt;").replaceAll(">", "&lt;");
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public boolean isValidUsername() {
		return validUsername;
	}

	public void setValidUsername(boolean validUsername) {
		this.validUsername = validUsername;
	}

	public String getUserRole() {
		return userRole;
	}

	public void setUserRole(String userRole) {
		this.userRole = userRole;
	}

	public String getNewUserRole() {
		return newUserRole;
	}

	public void setNewUserRole(String newUserRole) {
		this.newUserRole = newUserRole;
	}

	public String changeRole() {
		try {
			UserTicketsService.setRole(
					UserTicketsService.getUserId(this.username),
					Integer.parseInt(this.newUserRole));

			setMessage("������ �� ����������� � ������� �������!");
		} catch (SQLException e) {
			setError("�������� ������ - �������� ������!");
		}

		return "admin";
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error = error;
	}
}